$(function () {
	$(".logout").click(function () {
		var token = sessionStorage.getItem("token");
		var userId = sessionStorage.getItem("id");
		axios
			.get(`http://localhost:9000/users/logout?id=${userId}`, {
				headers: {
					Authorization: token,
				},
			})
			.then(function (response) {
				if (response.data.code === 1) {
					sessionStorage.clear();
					window.location.href = "./index.html";
				}
			})
			.catch(function (error) {
				console.error("error:", error);
			});
	});
	var nickName = sessionStorage.getItem("nickname");
	// 获取界面元素
	var offElement = document.querySelector(".off");
	var onElement = document.querySelector(".on");
	var nicknameElement = document.querySelector(".nickname");
	document.querySelector(".self").addEventListener("click", function () {
		window.location.href = "./self.html";
	});

	// 根据用户昵称状态更新界面
	if (!nickName) {
		// 用户未登录，激活未登录状态
		offElement.classList.add("active");
		onElement.classList.remove("active");
	} else {
		// 用户已登录，激活登录状态并显示昵称
		onElement.classList.add("active");
		offElement.classList.remove("active");
		nicknameElement.textContent = nickName;
	}

	// 获取轮播图数据
	axios
		.get("http://localhost:9000/carousel/list")
		.then(function (response) {
			if (response.data.code === 1) {
				var imagesHtml = response.data.list
					.map(function (item) {
						return `<div><img src="http://localhost:9000/${item.name}" /></div>`;
					})
					.join("");
				$("#carousel div").html(imagesHtml);
				layui.carousel.render({
					elem: "#carousel",
					width: "1200px",
					height: "600px",
					arrow: "hover",
					anim: "fade",
				});
			}
		})
		.catch(function (error) {
			console.error("error:", error);
		});
});
